package org.dromara.business.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.OrphanDetails;
import org.dromara.business.domain.VolunteerDetails;
import org.dromara.business.domain.bo.VolunteerDetailsBo;
import org.dromara.business.domain.vo.BusinessConfigVo;
import org.dromara.business.domain.vo.OrphanDetailsVo;
import org.dromara.business.domain.vo.VolunteerDetailsVo;
import org.dromara.business.mapper.VolunteerDetailsMapper;
import org.dromara.business.service.BusinessConfigService;
import org.dromara.business.service.VolunteerDetailsService;
import org.dromara.common.core.service.UserService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysUser;
import org.dromara.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 志愿者详细信息Service业务层处理
 *
 * @author zlj
 * @date 2025-01-18
 */
@RequiredArgsConstructor
@Service
public class VolunteerDetailsServiceImpl extends ServiceImpl<VolunteerDetailsMapper, VolunteerDetails> implements VolunteerDetailsService {

    private final VolunteerDetailsMapper baseMapper;
    private final BusinessConfigService businessConfigService;
    private final ISysUserService userService;

    /**
     * 查询志愿者详细信息
     */
    @Override
    public VolunteerDetailsVo queryById(Long userId) {
        VolunteerDetails volunteerDetails = baseMapper.selectById(userId);
        VolunteerDetailsVo volunteerDetailsVo = MapstructUtils.convert(volunteerDetails, VolunteerDetailsVo.class);

        translate(volunteerDetailsVo);
        return volunteerDetailsVo;
    }

    private void translate(VolunteerDetailsVo vo) {

        if (null == vo) {
            return;
        }
//        if(StrUtil.isNotBlank(volunteerDetailsVo.getName())){}

    }

    /**
     * 查询志愿者详细信息列表
     */
    @Override
    public TableDataInfo<VolunteerDetailsVo> queryPageList(VolunteerDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VolunteerDetails> lqw = buildQueryWrapper(bo);
        Page<VolunteerDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询志愿者详细信息列表
     */
    @Override
    public List<VolunteerDetailsVo> queryList(VolunteerDetailsBo bo) {
        LambdaQueryWrapper<VolunteerDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VolunteerDetails> buildQueryWrapper(VolunteerDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VolunteerDetails> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), VolunteerDetails::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getSex()), VolunteerDetails::getSex, bo.getSex());
        lqw.eq(bo.getAge() != null, VolunteerDetails::getAge, bo.getAge());
        lqw.eq(StrUtil.isNotBlank(bo.getNationality()), VolunteerDetails::getNationality, bo.getNationality());
        lqw.eq(StrUtil.isNotBlank(bo.getTel()), VolunteerDetails::getTel, bo.getTel());
        lqw.eq(StrUtil.isNotBlank(bo.getWorkUnit()), VolunteerDetails::getWorkUnit, bo.getWorkUnit());
        lqw.eq(StrUtil.isNotBlank(bo.getPoliticalStatus()), VolunteerDetails::getPoliticalStatus, bo.getPoliticalStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getCommunity()), VolunteerDetails::getCommunity, bo.getCommunity());
        lqw.eq(StrUtil.isNotBlank(bo.getMaritalStatus()), VolunteerDetails::getMaritalStatus, bo.getMaritalStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getChildrenSex()), VolunteerDetails::getChildrenSex, bo.getChildrenSex());
        lqw.eq(bo.getChildrenNum() != null, VolunteerDetails::getChildrenNum, bo.getChildrenNum());
        lqw.eq(StrUtil.isNotBlank(bo.getIsEnterprises()), VolunteerDetails::getIsEnterprises, bo.getIsEnterprises());
        lqw.eq(StrUtil.isNotBlank(bo.getIsVolunteer()), VolunteerDetails::getIsVolunteer, bo.getIsVolunteer());
        lqw.eq(StrUtil.isNotBlank(bo.getIsSociety()), VolunteerDetails::getIsSociety, bo.getIsSociety());
        lqw.eq(StrUtil.isNotBlank(bo.getSocietyInfo()), VolunteerDetails::getSocietyInfo, bo.getSocietyInfo());
        lqw.eq(StrUtil.isNotBlank(bo.getIsTradition()), VolunteerDetails::getIsTradition, bo.getIsTradition());
        lqw.eq(StrUtil.isNotBlank(bo.getTraditionInfo()), VolunteerDetails::getTraditionInfo, bo.getTraditionInfo());
        lqw.eq(StrUtil.isNotBlank(bo.getIsBelief()), VolunteerDetails::getIsBelief, bo.getIsBelief());
        lqw.eq(StrUtil.isNotBlank(bo.getBelief()), VolunteerDetails::getBelief, bo.getBelief());
        lqw.eq(StrUtil.isNotBlank(bo.getHelpNeeds()), VolunteerDetails::getHelpNeeds, bo.getHelpNeeds());
        lqw.eq(StrUtil.isNotBlank(bo.getHelpNeedsInfo()), VolunteerDetails::getHelpNeedsInfo, bo.getHelpNeedsInfo());
        lqw.eq(StrUtil.isNotBlank(bo.getIsParty()), VolunteerDetails::getIsParty, bo.getIsParty());
        lqw.orderByDesc(VolunteerDetails::getCreateTime);
        return lqw;
    }

    /**
     * 新增志愿者详细信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(VolunteerDetailsBo bo) {
        VolunteerDetails add = MapstructUtils.convert(bo, VolunteerDetails.class);
        validEntityBeforeSave(add);
        //
        if (null == bo.getPicture()) {
            if ("0".equals(bo.getSex())) {
                List<BusinessConfigVo> randomPictureMaleOrphan = businessConfigService.queryByKey("random_picture_male_orphan");
                String configValue = randomPictureMaleOrphan.get(0).getConfigValue();
                add.setPicture(configValue);
            } else {
                List<BusinessConfigVo> randomPictureMaleOrphan = businessConfigService.queryByKey("random_pictures_female_volunteers");
                String configValue = randomPictureMaleOrphan.get(0).getConfigValue();
                add.setPicture(configValue);
            }
        }
        userService.lambdaUpdate().set(SysUser::getType, 2).eq(SysUser::getUserId, add.getUserId()).update();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改志愿者详细信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(VolunteerDetailsBo bo) {
        VolunteerDetails update = MapstructUtils.convert(bo, VolunteerDetails.class);
        validEntityBeforeSave(update);
        SysUser user = userService.getById(bo.getUserId());
        if ("2".equals(bo.getState())){
            user.setType("2");
        }
        boolean b = userService.updateById(user);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(VolunteerDetails entity) {

    }

    /**
     * 批量删除志愿者详细信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
